package com.newworld.dmsapp2.fragment.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.fragment.bean.Message;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.StringUtil;

/**
 * @author long 主页面消息的adapter
 * 
 */
public class MessageFragementAdapter extends BaseAdapter {

	private List<Message> messageList;
	private LayoutInflater inflater;

	public MessageFragementAdapter(Context context, List<Message> messageList) {
		if(null != context && null != LayoutInflater.from(context)){
			inflater = LayoutInflater.from(context);
			this.messageList = messageList;
		}
		

	}

	@Override
	public int getCount() {
		if(null != messageList){
			return messageList.size();
		}else{
			return 0;
		}
		
	}

	@Override
	public Object getItem(int position) {
		if(null != messageList){
			return messageList.get(position);
		}else{
			return 0;
		}
		
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.fragment_main_message_item,
					parent, false);
			holder = new ViewHolder();
			holder.locationTv = (TextView) convertView
					.findViewById(R.id.tv_message_item_location);
			holder.fromPeopleTv = (TextView) convertView
					.findViewById(R.id.tv_message_item_people_from);
			holder.statusTv = (TextView) convertView
					.findViewById(R.id.tv_message_item_status);
			holder.timeTv = (TextView) convertView
					.findViewById(R.id.tv_message_item_time);
			holder.reddotImv = (ImageView)convertView.findViewById(R.id.iv_message_item_reddot);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Message item = messageList.get(position);
		holder.locationTv.setText(item.getTitle());
		holder.fromPeopleTv.setText(item.getFrom());
		holder.statusTv.setText(item.getContent());
		holder.timeTv.setText(item.getGenerateTime().substring(0,
				Config.TIMELENGTH));
		if(StringUtil.isStringEqual(item.getMessageStatusType(), Config.MESSAGE_STATUS_READED)){
			holder.reddotImv.setVisibility(View.INVISIBLE);
		}else{
			holder.reddotImv.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

	private class ViewHolder {

		TextView locationTv;
		TextView fromPeopleTv;
		TextView statusTv;
		TextView timeTv;
		ImageView reddotImv;
	}
}